﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using OxyPlot;
using OxyPlot.Axes;
using OxyPlot.Series;
using DetectingKinectSencor._40_Tools;
using OxyPlot.Annotations;
using DetectingKinectSencor._10_ColorStream;
using System.IO;
using System.Windows.Media.Imaging;
using System.Drawing;

namespace DetectingKinectSencor._50_Evaluation.ViewModels.Dashboard
{
    public class GesturesViewModelDashboard : Notifier
    {
        // Internal variable in which the model is saved in this class
        private PlotModel plotModel;

        public PlotModel tempPlotModel;
        public PlotModel tempPlotModel2;

        private LineSeries timepointSerieOpenGesturesList;
        private LineSeries timepointSerieHandsBelowWaistlineList;

        private DateTime captureTimeLastSequence;

        // Important the UI needs to bind the Model to MovingPlotModel!!!
        public PlotModel GesturesPlotModel
        {
            get { return plotModel; }
            set { plotModel = value; RaisePropertyChanged(() => GesturesPlotModel); }
        }

        /// <summary>
        /// Used in the Dashboard
        /// </summary>
        /// <param name="lastElementCaptureTime"></param>
        public GesturesViewModelDashboard(DateTime lastElementCaptureTime)
        {
            tempPlotModel = new PlotModel();
            SetUpModel();
            LoadData(lastElementCaptureTime);
        }

        private void SetUpModel()
        {
            // Define the legend for the date/time graph

            tempPlotModel.LegendTitle = "Legend";
            tempPlotModel.LegendOrientation = LegendOrientation.Horizontal;
            tempPlotModel.LegendPlacement = LegendPlacement.Outside;
            tempPlotModel.LegendPosition = LegendPosition.TopRight;
            tempPlotModel.LegendBackground = OxyColor.FromAColor(200, OxyColors.White);
            tempPlotModel.LegendBorder = OxyColors.Black;
            tempPlotModel.LegendFontSize = DesignOfGraphs.fontSizeDashboard;

            TimeSpanAxis timeLineAxis = new TimeSpanAxis(AxisPosition.Bottom, "Timeline (mm:ss)", "mm:ss")
                                                        {
                                                            MajorGridlineStyle = LineStyle.Solid,
                                                            MinorGridlineStyle = LineStyle.Dot,
                                                            FontSize = DesignOfGraphs.fontSizeDashboard
                                                        };
            tempPlotModel.Axes.Add(timeLineAxis);
            LinearAxis valueAxis = new LinearAxis(AxisPosition.Left, 0, 3, "Detections identifier")
                                                 {
                                                     MajorGridlineStyle = LineStyle.Solid,
                                                     MinorGridlineStyle = LineStyle.Dot,
                                                     MinorStep = 0.5,
                                                     MajorStep = 1,
                                                     FontSize = DesignOfGraphs.fontSizeDashboard
                                                 };
            tempPlotModel.Axes.Add(valueAxis);

        }
      

        public void LoadData(DateTime captureTime)
        {
            // Get the list with the viewingDirectionData
            OpenGestureData openGestureList = EvaluationData.openGestureData;
            HandsBelowWaistlineData handsBelowWaistlineList = EvaluationData.handsBelowWaistlineData;
            TimeSpan elapsedTime;

            this.captureTimeLastSequence = captureTime;

            // Shows the data time graph

            timepointSerieOpenGesturesList = new LineSeries
            {
                StrokeThickness = 2,
                MarkerSize = 3,
                MarkerStroke = Graph.colors[1],
                MarkerType = Graph.markerTypes[1],
                CanTrackerInterpolatePoints = false,
                Title = string.Format("Open Gestures"),
                Smooth = false,
            };

            timepointSerieHandsBelowWaistlineList = new LineSeries
            {
                StrokeThickness = 2,
                MarkerSize = 3,
                MarkerStroke = Graph.colors[2],
                MarkerType = Graph.markerTypes[2],
                CanTrackerInterpolatePoints = false,
                Title = string.Format("Hands below waistline"),
                Smooth = false,
            };




            // Add the data points openGesture

            foreach (CaptureTime openGesture in openGestureList.trackingSequences)
            {
                if (captureTimeLastSequence == openGesture.captureTime)
                {
                    for (int i = 0; i < openGesture.timePoints.Count; i++)
                    {
                        // It is more intuitiv to see the actual elapsed time from the beginning then the actual time 
                        elapsedTime = openGesture.timePoints[i].timePoint - captureTimeLastSequence;
                        timepointSerieOpenGesturesList.Points.Add(new DataPoint(
                                                                      TimeSpanAxis.ToDouble(elapsedTime),
                                                                      1));
                    }

                    // Add the time point to the date/time graph
                    tempPlotModel.Series.Add(timepointSerieOpenGesturesList);

                    break;
                }
            }

            // Add the data points handsBelowWaistline
            foreach (CaptureTime handsBelowWaistline in handsBelowWaistlineList.trackingSequences)
            {
                if (captureTimeLastSequence == handsBelowWaistline.captureTime)
                {
                    for (int i = 0; i < handsBelowWaistline.timePoints.Count; i++)
                    {
                        // It is more intuitiv to see the actual elapsed time from the beginning then the actual time 
                        elapsedTime = handsBelowWaistline.timePoints[i].timePoint - captureTimeLastSequence;
                        timepointSerieHandsBelowWaistlineList.Points.Add(new DataPoint(
                                                                             TimeSpanAxis.ToDouble(elapsedTime),
                                                                             2));
                    }

                    // Add the time point to the date/time graph
                    tempPlotModel.Series.Add(timepointSerieHandsBelowWaistlineList);

                    break;
                }
            }


            //Saved the tempPlotModel to the plotModel than is the OnPropertyChanged executed and the UI is updated
            GesturesPlotModel = tempPlotModel;

            
        }
    }
}





